/***************************************************************************
 *                                                                         *
 *   This file is part of the CrystalMem project,                          *
 *       http://code.google.com/p/crystalmem                               *
 *                                                                         *
 *   Copyright (C) 2008 by Alberto Scarpa <scarpa.alberto@gmail.com>       *
 *   Copyright (C) 2008 by Enrico Ros <enrico.ros@gmail.com>               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "WidgetItem.h"
#include <QPainter>
#include <QGLContext>

// TODO add the pivot point? (def: 0,0)

WidgetItem::WidgetItem( QGraphicsItem * parent )
    : QGraphicsItem( parent )
    , m_size( 200, 150 )
{
}

void WidgetItem::move( qreal x, qreal y )
{
    setPos( x, y );
}

void WidgetItem::move( const QPointF & point )
{
    setPos( point );
}

void WidgetItem::resize( qreal width, qreal height )
{
    // check the minimumSizeHint
    QSizeF minSize = minimumSizeHint();
    if ( minSize.isValid() ) {
        if ( minSize.width() > width )
            width = minSize.width();
        if ( minSize.height() > height )
            height = minSize.height();
    }

    // check that it's at least zero
    if ( width < 0 )
        width = 0;
    if ( height < 0 )
        height = 0;

    // optimize
    if ( m_size.width() == width && m_size.height() == height )
        return;

    // perform resizing and associated repainting
    prepareGeometryChange();
    m_size = QSizeF( width, height );
    resizeEvent( m_size );
    update();
}

void WidgetItem::resize( const QSizeF & size )
{
    resize( size.width(), size.height() );
}

float WidgetItem::width() const
{
    return m_size.width();
}

float WidgetItem::height() const
{
    return m_size.height();
}

QSizeF WidgetItem::size() const
{
    return m_size;
}

QRectF WidgetItem::boundingRect() const
{
    return QRectF( 0, 0, m_size.width(), m_size.height() );
}

// temp/virtual stubs
void WidgetItem::paint( QPainter * painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/ )
{
#if 1
    Q_UNUSED( painter );
    QRect br = boundingRect().toRect().translated( x(), y() );
    glEnable( GL_BLEND );
    glBegin( GL_QUADS );
        glColor4d( 0.7, 0, 0, 0.25 );
        glVertex2f( br.left(), br.top() );
        glVertex2f( br.right(), br.top() );
        glVertex2f( br.right(), br.bottom() );
        glVertex2f( br.left(), br.bottom() );
    glEnd();
    glDisable( GL_BLEND );
#else
    painter->fillRect( boundingRect(), QColor( 200, 0, 0, 64 ) );
#endif
}

QSizeF WidgetItem::minimumSizeHint() const
{
    return QSizeF();
}

void WidgetItem::resizeEvent( const QSizeF & /*size*/ )
{
    // dummy impl
}

